package my.dr.controller;

import lombok.extern.slf4j.Slf4j;
import my.dr.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@Controller
public class BaseController {
    @Autowired
    HttpServletRequest request;
    @Autowired
    HttpServletResponse response;
    @Autowired
    RedisUtil redisUtil;

    /**
     * 获取客户端ip
     * @return ip地址
     */
    public String getIp() {
        String ip = request.getHeader("x-forwarded-for");
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }
    /**
     * 通过ua获取系统
     * @return 系统名称
     */
    public  String getOs() {
        String userAgent = request.getHeader("user-agent");
        if (userAgent.length() < 4) {
            return "其他系统";
        }
        if (userAgent.contains("Windows")) {
            if (userAgent.contains("Windows NT 10.0")) {
                return "Windows 10";
            } else if (userAgent.contains("Windows NT 6.2")) {
                return "Windows 8";
            } else if (userAgent.contains("Windows NT 6.1")) {
                return "Windows 7";
            } else if (userAgent.contains("Windows NT 6.0")) {
                return "Windows Vista";
            } else if (userAgent.contains("Windows NT 5.2")) {
                return "Windows XP";
            } else if (userAgent.contains("Windows NT 5.1")) {
                return "Windows XP";
            } else if (userAgent.contains("Windows NT 5.01")) {
                return "Windows 2000";
            } else if (userAgent.contains("Windows NT 5.0")) {
                return "Windows 2000";
            } else if (userAgent.contains("Windows NT 4.0")) {
                return "Windows NT 4.0";
            } else if (userAgent.contains("Windows 98; Win 9x 4.90")) {
                return "Windows ME";
            } else if (userAgent.contains("Windows 98")) {
                return "Windows 98";
            } else if (userAgent.contains("Windows 95")) {
                return "Windows 95";
            } else if (userAgent.contains("Windows CE")) {
                return "Windows CE";
            }
        } else if (userAgent.contains("Mac OS X")) {
            if (userAgent.contains("iPhone")) {
                return "iPhone";
            } else if (userAgent.contains("iPad")) {
                return "iPad";//判断系统
            } else {
                return "Mac";//判断系统
            }
        } else if (userAgent.contains("Android")) {
            return "Android";//判断系统
        } else if (userAgent.contains("Ubuntu")) {
            return "Ubuntu";//判断系统
        } else if (userAgent.contains("Linux")) {
            return "Linux";//判断系统
        } else if (userAgent.contains("FreeBSD")) {
            return "FreeBSD";//判断系统
        } else if (userAgent.contains("Solaris")) {
            return "Solaris";//判断系统
        }
        return "其他系统";
    }
    /**
     * 通过ua识别浏览器
     * @return 浏览器
     */
    public String getBrowser() {
        String userAgent = request.getHeader("user-agent");
        if (userAgent.length() < 4) {
            return "其他浏览器";
        }
        if (userAgent.contains("Edge")) {
            return "Microsoft Edge";
        } else if (userAgent.contains("QQBrowser")) {
            return "QQ浏览器";
        } else if (userAgent.contains("Chrome") && userAgent.contains("Safari")) {
            return "Chrome";
        } else if (userAgent.contains("Firefox")) {
            return "Firefox";
        } else if (userAgent.contains("360")) {//Internet Explorer 6
            return "360浏览器";
        } else if (userAgent.contains("Opera")) {//Internet Explorer 6
            return "Opera";
        } else if (userAgent.contains("Safari") && !userAgent.contains("Chrome")) {
            return "Safari";
        } else if (userAgent.contains("Sogou")) {//Internet Explorer 6
            return "搜狗浏览器";
        } else if (userAgent.contains("TencentTraveler")) {//Internet Explorer 6
            return "腾讯浏览器";
        } else if (userAgent.contains("UC")) {//Internet Explorer 6
            return "UC浏览器";
        } else if (userAgent.contains("MicroMessenger/7")) {//Internet Explorer 6
            return "微信访问";
        } else if (userAgent.contains("TIM")) {//Internet Explorer 6
            return "TIM访问";
        } else if (userAgent.contains("QQ")) {//Internet Explorer 6
            return "QQ访问";
        } else if (userAgent.contains("Maxthon")) {//Internet Explorer 6
            return "遨游浏览器";
        } else if (userAgent.contains("Quark")) {//Internet Explorer 6
            return "夸克浏览器";
        } else if (userAgent.contains("Build")) {//Internet Explorer 6
            return "百度浏览器";
        } else if (userAgent.contains("2345")) {//Internet Explorer 6
            return "2345浏览器";
        } else if (userAgent.contains("Zeus")) {//Internet Explorer 6
            return "宙斯浏览器";
        } else if (userAgent.contains("LieBao")) {//Internet Explorer 6
            return "猎豹浏览器";
        } else if (userAgent.contains("Vivo")) {//Internet Explorer 6
            return "vivo浏览器";
        }  else if (userAgent.contains("Tao")) {//Internet Explorer 6
            return "淘宝浏览器";
        } else if (userAgent.contains("Vivo")) {//Internet Explorer 6
            return "vivo浏览器";
        }
        else if (userAgent.contains("MSIE")) {
            if (userAgent.contains("MSIE 10.0")) {//Internet Explorer 10
                return "IE 10";
            } else if (userAgent.contains("MSIE 9.0")) {//Internet Explorer 9
                return "IE 9";
            } else if (userAgent.contains("MSIE 8.0")) {//Internet Explorer 8
                return "IE 8";
            } else if (userAgent.contains("MSIE 7.0")) {//Internet Explorer 7
                return "IE 7";
            } else if (userAgent.contains("MSIE 6.0")) {//Internet Explorer 6
                return "IE 6";
            }
        }
        return "其他浏览器";
    }
    /**
     * 获取session内容
     * @param name 键
     * @return 值
     */
    public Object getObjectAttr(String name){
        return this.request.getSession().getAttribute(name);
    }
    /**
     * 获取session内容
     * @param name 键
     * @return 值
     */
    public String getStringAttr(String name){
        return this.getObjectAttr(name).toString();
    }
    /**
     * 获取session内容
     * @param name 键
     * @return 值
     */
    public int getIntAttr(String name){
        return (int) this.getObjectAttr(name);
    }
    /**
     * 设置session内容
     * @param key 键
     * @param obj 值
     * @return 返回对象
     */
    public BaseController setAttr(String key,Object obj){
        request.getSession().setAttribute(key,obj);
        return this;
    }
    /**
     * 删除session内容
     * @param name 键
     */
    public void removeAttr(String name){
        this.request.getSession().removeAttribute(name);
    }
    /**
     * 获取全部上传的文件
     * @return 文件map
     */
    public Map<String, MultipartFile> getFiles(){
        MultipartHttpServletRequest mRequest = (MultipartHttpServletRequest) request;
        return mRequest.getFileMap();
    }
    /**
     * 获取上传的文件
     * @param name key名字
     * @return 文件
     */
    public MultipartFile getFiles(String name){
        MultipartHttpServletRequest mRequest = (MultipartHttpServletRequest) request;
        return mRequest.getFileMap().get(name);
    }
}
